(function(){
  // 坐标及宽高大小可能会涉及到dpr
  var dpr = window.devicePixelRatio || 1;

  var ua = window.navigator.userAgent.toLocaleLowerCase();
  if (ua.indexOf('iphone') > -1 || ua.indexOf('ipod') > -1 || ua.indexOf('ipad') > -1) {
    var iOS = true;
  } else {
    var iOS = false;
  }

  window.webJSBridge = {};
  window.peach = {};

  // APP 注入方法：window.appJSBridge
  // Web端方法：window.webJSBridge

  // 修改web页面字体大小
  // size取值：xx-small（最小）、x-small（较小）、small（小）、medium（正常）、large（大）、x-large（较大）、xx-large（最大）
  window.webJSBridge.fontSize = function(size){
    $('.j-pagescreen-detail').removeClass('app-fontsize-xx-small app-fontsize-x-small app-fontsize-small app-fontsize-medium app-fontsize-large app-fontsize-x-large app-fontsize-xx-large');
    $('.j-pagescreen-detail').addClass('app-fontsize-'+size);
  };
  window.peach.fontSize = window.webJSBridge.fontSize;

  // 标准弹出框：
  window.peach.alert = function(text){
    window.appJSBridge.alert(text);
  };

  // 即逝弹出框：
  window.peach.toast = function(text){
    window.appJSBridge.toast(text);
  };



  window.webJSBridge.confirm = function(jsonData){//按钮值{'status' : 'xxx'}
    // nothing
  };

  //二次确认框
  window.peach.confirm = function(param){
    var  data = {
      'title': param['title'] || '确认框',
      'message': param['message'] || '确认是否清空？',
      'btnSure': param['btnSure'] || '确认',
      'btnCancel': param['btnCancel'] || '取消',
      'sureValue': param['sureValue'] || 'sure',
      'cancelValue': param['cancelValue'] || 'cancel'
    };
    if (param['sureFunc']) window.webJSBridge.confirm = param['sureFunc'];
    window.appJSBridge.confirm(JSON.stringify(data));
  };



  // 打开新webView
  window.peach.openUrl = function(url){
    window.appJSBridge.openUrl(url);
  };

  // 打开外链
  window.peach.openBrowser = function(url){
    window.appJSBridge.openBrowser(url);
  };

  // 打开APP某界面
  window.peach.openScreen = function(param){
    window.appJSBridge.openScreen(JSON.stringify(param));
  };

  // 从APP获取数据
  window.peach.getData = function(param, callback){
    window.appJSBridge.getData(JSON.stringify(param));
    // 回调
    if (callback) window.webJSBridge.setData = callback;
  };

  window.webJSBridge.setData = function(jsonData){
    // var data = JSON.parse(jsonData);
  };

  // 打开新闻
  window.peach.openNews = function(param){
    window.appJSBridge.openNews(JSON.stringify(param));
  };

  // 刷新页面
  window.webJSBridge.refresh = function(){
    location.reload();
  };

  // 打开评论
  window.peach.openComment = function(param){
    window.appJSBridge.openComment(JSON.stringify(param));
  };

  // 刷新页面评论
  window.webJSBridge.refreshComment = function(){};
  window.peach.refreshComment = function(callback){
    if (callback) window.webJSBridge.refreshComment = callback;
    window.appJSBridge.refreshComment();
  };

  // 是否登录：0 未登录、1 已登录
  window.webJSBridge.loginState = function(){};
  window.peach.isLogin = function(callback){
    if (callback) window.webJSBridge.loginState = callback;
    window.appJSBridge.isLogin();
  };

  // 获取App信息
  // userId(0表示未登录)、userName、clientLabel、network(网络状态)、appVersion、fontSize、wordMode(纯文无图模式)
  // token 信息
  window.webJSBridge.appInfo = function(jsonData){};
  window.peach.getAppInfo = function(callback){
    if (callback) window.webJSBridge.appInfo = callback;
    window.appJSBridge.getAppInfo();
  };

  // 登录
  window.webJSBridge.loginSuccess = function(jsonData){
    // nothing
  };
  window.peach.loginSuccess = function(callback){
    if (callback) window.webJSBridge.loginSuccess = callback;
  };
  // success、error、cancel
  window.peach.login = function(param){
    if (param['success']) window.webJSBridge.loginSuccess = param['success'];
    if (param['error']) window.webJSBridge.loginError = param['error'];
    if (param['cancel']) window.webJSBridge.loginCancel = param['cancel'];
    window.appJSBridge.login();
  };

  // 分享
  // docType、title、desc、link、imgUrl
  window.peach.share = function(param){
    if (param['success']) window.webJSBridge.shareSuccess = param['success'];
    if (param['error']) window.webJSBridge.shareError = param['error'];
    if (param['cancel']) window.webJSBridge.shareCancel = param['cancel'];
    window.appJSBridge.share();
  };

  // 图片浏览
  // index、imgList（imgUrl、imgTitle）
  window.peach.previewImage = function(param){
    window.appJSBridge.previewImage(JSON.stringify(param));
  };

  // 全屏播放视频
  window.peach.playVideoFull = function(url){
    window.appJSBridge.playVideoFull(url);
  };

  // 窗口播放视频
  // url、poster、offsetX、offsetY、width、height
  window.peach.playVideo = function(param){
    if (iOS) {
      ['offsetX', 'offsetY', 'width', 'height'].forEach(function(key){
        param[key] = param[key] / dpr;
      });
    }
    window.appJSBridge.playVideo(JSON.stringify(param));
  };

  // 全屏播放VR视频
  window.peach.playVrFull = function(url){
    window.appJSBridge.playVrFull(url);
  };

  // 窗口播放VR视频
  // url、poster、offsetX、offsetY、width、height
  window.peach.playVr = function(param){
    if (iOS) {
      ['offsetX', 'offsetY', 'width', 'height'].forEach(function(key){
        param[key] = param[key] / dpr;
      });
    }
    window.appJSBridge.playVr(JSON.stringify(param));
  };

  // 语音播报
  window.peach.readNews = function(content){
    window.appJSBridge.readNews(content);
  };
  window.peach.readNewsPlay = function(){
    window.appJSBridge.readNewsPlay();
  };
  window.peach.readNewsPaused = function(){
    window.appJSBridge.readNewsPaused();
  };
  window.peach.readNewsStop = function(){
    window.appJSBridge.readNewsStop();
  };
  window.webJSBridge.readNewsEnd = function(){};
  window.webJSBridge.readNewsError = function(){};
  window.webJSBridge.readNewsStatus = function(jsonData){

  }

  window.peach.readNewsEnd = function(callback){
    if (callback) window.webJSBridge.readNewsEnd = callback;
  };
  window.peach.readNewsError = function(callback){
    if (callback) window.webJSBridge.readNewsError = callback;
  };

  window.peach.readNewsStatus = function(callback){
    if (callback) window.webJSBridge.readNewsStatus = callback;
  };

  // 选取相册照片
  // count（数量）、sourceType（照片源，数组格式）：album（相册）、camera（相机）
  window.peach.chooseImage = function(param, success, cancel){
    if (success) window.webJSBridge.chooseImageSuccess = success;
    if (cancel) window.webJSBridge.chooseImageCancel = success;
    window.appJSBridge.chooseImage(JSON.stringify(param));
  };

  // App状态变化
  window.peach.webSleep = function(callback){
    if (callback) window.appJSBridge.webSleep = callback;
  };
  window.peach.webWake = function(callback){
    if (callback) window.appJSBridge.webWake = callback;
  };
  window.peach.webClose = function(callback){
    if (callback) window.appJSBridge.webClose = callback;
  };

  // 临时情况使用
  window.peach.explore = function(param, callback){
    window.appJSBridge.explore(JSON.stringify(param));
    if (callback) window.webJSBridge.explore = callback;
  };

  window.peach.hiddenAppbar = function(){
    window.appJSBridge.hiddenAppbar();
  };
}());
